//
//  ViewController.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/15.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "ViewController.h"
#import "mainView.h"
#import "monitorView.h"
#import "contextualView.h"
#import "myView.h"

#import "XNTarBar.h"

@interface ViewController () <mainViewDelegate, sceneDelegate>
{
    XNTarBar *myTabView;
    CGFloat navigationViewHeight;
    CGFloat tabBarHeight;
    NSDictionary *jsonDic;
    int showTableView;
    BOOL isShowAddSceneFundationView;
}

@property (nonatomic, strong) mainView *HomeView;
@property (nonatomic, strong) monitorView *CameraView;
@property (nonatomic, strong) contextualView *SceneView;
@property (nonatomic, strong) myView *MyView;

@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UILabel *navigationTitle;
@property (nonatomic, strong) UIButton *navigationLeftButton;
@property (nonatomic, strong) UIButton *navigationRightButton;
@property (nonatomic, strong) UIButton *sceneViewRightButton;

@property (nonatomic, weak) UIButton *selectedBtn;

@end

@implementation ViewController

- (mainView *)HomeView
{
    if(!_HomeView){
        _HomeView = [[mainView alloc] initWithFrame:CGRectMake(0, navigationViewHeight,
                                                               self.view.bounds.size.width,
                                                               self.view.bounds.size.height - navigationViewHeight) andDic:jsonDic];
        _HomeView.delegate = self;
        __weak ViewController *vc = self;
        _HomeView.sendMessage = ^(NSString *message1, NSString *message2){
            vc.sendMessage(message1, message2);
        };
    }
    return _HomeView;
}

- (monitorView *)CameraView
{
    if(!_CameraView){
        _CameraView = [[monitorView alloc] initWithFrame:CGRectMake(0, navigationViewHeight,
                                                                    self.view.bounds.size.width,
                                                                    self.view.bounds.size.height - navigationViewHeight)];
    }
    return _CameraView;
}

- (contextualView *)SceneView
{
    if(!_SceneView){
        _SceneView = [[contextualView alloc] initWithFrame:CGRectMake(0, navigationViewHeight,
                                                                      self.view.bounds.size.width,
                                                                      self.view.bounds.size.height - navigationViewHeight - tabBarHeight) andDic:jsonDic];
        _SceneView.sceneDelagate = self;
        __weak ViewController *vc = self;
        _SceneView.sendMessage = ^(NSString *message){
            vc.sendFullMessage(message);
        };
    }
    return _SceneView;
}

- (myView *)MyView
{
    if(!_MyView){
        _MyView = [[myView alloc] initWithFrame:CGRectMake(0, navigationViewHeight,
                                                           self.view.bounds.size.width,
                                                           self.view.bounds.size.height - tabBarHeight - navigationViewHeight)];
    }
    return _MyView;
}

- (UIView *)navigationView
{
    if(!_navigationView){
        navigationViewHeight = 60.0f;
        _navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, navigationViewHeight)];
        [_navigationView setBackgroundColor:TEXT_GREEN_COLOR];
    }
    return _navigationView;
}

- (UILabel *)navigationTitle
{
    if(!_navigationTitle){
        _navigationTitle = [[UILabel alloc] init];
        _navigationTitle.textColor = [UIColor whiteColor];
        _navigationTitle.textAlignment = NSTextAlignmentCenter;
        _navigationTitle.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    }
    return _navigationTitle;
}

- (UIButton *)navigationLeftButton
{
    if(!_navigationLeftButton){
        UIImage *image = GET_IAMGE_WITH_NAME(@"Image/navigation/btn_back");
        _navigationLeftButton = [[UIButton alloc] init];
        [_navigationLeftButton setImage:image forState:UIControlStateNormal];
    }
    return _navigationLeftButton;
}

- (UIButton *)navigationRightButton
{
    if(!_navigationRightButton){
        UIImage *image = GET_IAMGE_WITH_NAME(@"Image/navigation/ico_change_list");
        _navigationRightButton = [[UIButton alloc] init];
        [_navigationRightButton setImage:image forState:UIControlStateNormal];
    }
    return _navigationRightButton;
}

- (UIButton *)sceneViewRightButton
{
    if(!_sceneViewRightButton){
        _sceneViewRightButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navigationView.bounds.size.width - 45, 20, 40, 20)];
        _sceneViewRightButton.titleLabel.textAlignment = NSTextAlignmentRight;
        _sceneViewRightButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
        [_sceneViewRightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _sceneViewRightButton;
}

- (void)addNavigation
{
    [self.view addSubview:self.navigationView];
    [self.navigationTitle setFrame:CGRectMake(0, 0, 200, navigationViewHeight)];
    self.navigationTitle.center = CGPointMake(self.navigationView.bounds.size.width / 2, self.navigationView.bounds.size.height / 2);
    [self.navigationView addSubview:self.navigationTitle];
    
    [self.navigationLeftButton setFrame:CGRectMake(0, 0, navigationViewHeight / 2, navigationViewHeight / 2)];
    self.navigationLeftButton.center = CGPointMake(20, self.navigationView.bounds.size.height / 2);
    [self.navigationRightButton setFrame:CGRectMake(0, 0, navigationViewHeight / 2, navigationViewHeight / 2)];
    self.navigationRightButton.center = CGPointMake(self.navigationView.bounds.size.width - navigationViewHeight / 2, self.navigationView.bounds.size.height / 2);
    
    [self.navigationView addSubview:self.navigationLeftButton];
    [self.navigationView addSubview:self.navigationRightButton];
}

- (void)removeLeftButton
{
    [self.navigationLeftButton removeTarget:self action:@selector(showTabView) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationLeftButton removeTarget:self action:@selector(showSceneView) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationLeftButton removeTarget:self action:@selector(showContentView) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationLeftButton removeFromSuperview];
}

- (void)removeSceneRightButton
{
    [self.sceneViewRightButton removeTarget:self action:@selector(saveNewScene) forControlEvents:UIControlEventTouchUpInside];
    [self.sceneViewRightButton removeTarget:self action:@selector(addFundation) forControlEvents:UIControlEventTouchUpInside];
    [self.sceneViewRightButton removeFromSuperview];
}

- (void)addViewByNumber:(NSInteger)num
{
    if(num == 0){
        [self.view addSubview:self.HomeView];
        self.navigationTitle.text = @"主页";
        [self removeLeftButton];
        [self.navigationRightButton removeFromSuperview];
    }else if(num == 1){
        [self.view addSubview:self.CameraView];
        self.navigationTitle.text = @"视频监控列表";
        [self.navigationView addSubview:self.navigationRightButton];
        [self removeLeftButton];
    }else if(num == 2){
        [self.view addSubview:self.SceneView];
        self.navigationTitle.text = @"情景模式";
        [self removeLeftButton];
        [self.navigationRightButton removeFromSuperview];
    }else if(num == 3){
        [self.view addSubview:self.MyView];
        self.navigationTitle.text = @"我的";
        [self removeLeftButton];
        [self.navigationRightButton removeFromSuperview];
    }
    [myTabView removeFromSuperview];
    showTableView = 0;
    [self.view addSubview:myTabView];
}

#pragma mark - mainViewDelegate
- (void)showTitle:(NSString *)title
{
    if(![title isEqualToString:@"首页"]){
        showTableView++;
    }
    self.navigationTitle.text = title;
}

- (void)showLeftButton
{
    [self.navigationView addSubview:self.navigationLeftButton];
    [self.navigationLeftButton addTarget:self action:@selector(showTaleView) forControlEvents:UIControlEventTouchUpInside];
}

- (void)showTaleView
{
    if(showTableView == 1){
        [self hideLeftButton];
    }else if(showTableView == 2){
        [self.HomeView showDeviceTable];
        showTableView--;
    }
    showTableView--;
}

- (void)hideLeftButton
{
    [self.HomeView showFloorTable];
    [self removeLeftButton];
}

- (void)hideTabView
{
    myTabView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, myTabView.bounds.size.height);
}

- (void)showTabView
{
    myTabView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, 0);
}

#pragma mark - sceneViewDelegate
- (void)showSceneTitle:(NSString *)title
{
    self.navigationTitle.text = title;
}

- (void)showSceneLeftView
{
    [self.navigationView addSubview:self.navigationLeftButton];
    if(isShowAddSceneFundationView){
        [self.navigationLeftButton addTarget:self action:@selector(showContentView) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [self.navigationLeftButton addTarget:self action:@selector(showSceneView) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)hideSceneLeftView
{
    [self removeLeftButton];
}

- (void)showSceneRightButton:(NSUInteger)type
{
    if(type == 0){
        [self.sceneViewRightButton setTitle:@"保存" forState:UIControlStateNormal];
        [self.sceneViewRightButton addTarget:self action:@selector(saveNewScene) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationView addSubview:self.sceneViewRightButton];
    }else if(type == 1){
        [self.sceneViewRightButton setTitle:@"添加" forState:UIControlStateNormal];
        [self.sceneViewRightButton addTarget:self action:@selector(addFundation) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationView addSubview:self.sceneViewRightButton];
    }
}

- (void)hideSceneRightButton
{
    [self removeSceneRightButton];
}

- (void)showSceneView
{
    [self.SceneView hideNowView];
    [self showSceneTitle:@"情景模式"];
    [self hideSceneLeftView];
    [self hideSceneRightButton];
    [self showTabView];
}

- (void)showContentView
{
    [self.SceneView showSceneContentView];
}

- (void)setIsShowContentView:(BOOL)b
{
    isShowAddSceneFundationView = b;
}

- (void)saveNewScene
{
    [self.SceneView updateData];
}

- (void)addFundation
{
    [self hideSceneRightButton];
    [self hideLeftButton];
    isShowAddSceneFundationView = YES;
    [self showSceneTitle:@"添加功能"];
    [self showSceneLeftView];
    [self.SceneView addFundation];
}

#pragma mark - method
- (void)tabBar:(XNTarBar *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to
{
    if(from == 0){
        [self.HomeView removeFromSuperview];
        self.HomeView = nil;
    }else if(from == 1) {
        [self.CameraView removeFromSuperview];
        self.CameraView = nil;
    }else if(from == 2){
        [self.SceneView removeFromSuperview];
        self.SceneView = nil;
    }else{
        [self.MyView removeFromSuperview];
        self.MyView = nil;
    }
    [self addViewByNumber:to];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDic:(NSDictionary *)dic
{
    jsonDic = [[NSDictionary alloc] initWithDictionary:dic];
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    showTableView = 0;
    isShowAddSceneFundationView = NO;
    [self addNavigation];
    self.view.userInteractionEnabled = YES;
    
    [self.tabBar removeFromSuperview];
    myTabView = [[XNTarBar alloc] init];
    myTabView.backgroundColor = BACKGROUND_COLOR;
    
    __weak ViewController *vc = self;
    __weak XNTarBar *tview = myTabView;
    myTabView.selectTabBar = ^(NSInteger from, NSInteger to){
        [vc tabBar:tview selectedFrom:from to:to];
    };
    
    tabBarHeight = 70.0f;
    myTabView.frame = CGRectMake(0, self.view.bounds.size.height - tabBarHeight, self.view.bounds.size.width, tabBarHeight);
    [self.view addSubview:myTabView];
    
    
    for (int i = 0; i < 4; i++) {
        UIImage *image, *selectedImage;
        if(i == 0){
            image = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_home_n");
            selectedImage = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_home_p");
            [myTabView addButtonWithImage:image selectedImage:selectedImage andTitlt:@"首页"];
        }else if(i == 1){
            image = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_camera_n");
            selectedImage = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_camera_p");
            [myTabView addButtonWithImage:image selectedImage:selectedImage andTitlt:@"监控"];
        }else if(i == 2){
            image = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_scene_n");
            selectedImage = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_scene_p");
            [myTabView addButtonWithImage:image selectedImage:selectedImage andTitlt:@"情景模式"];
        }else{
            image = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_my_n");
            selectedImage = GET_IAMGE_WITH_NAME(@"Image/tabBar/nav_my_p");
            [myTabView addButtonWithImage:image selectedImage:selectedImage andTitlt:@"我的"];
        }
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end
